/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.isen.web.webservicetemp.controller;

import fr.isen.web.webservicetemp.Entity.Data;

import fr.isen.web.webservicetemp.Entity.DataSample;
import fr.isen.web.webservicetemp.Entity.Temperature;
import fr.isen.web.webservicetemp.Service.DataService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Maistre Louis
 */
@Controller
public class JSONController {

    @Autowired
    DataService dataService;

    @RequestMapping(value = "/Json", method = RequestMethod.GET)
    public @ResponseBody
    DataSample getEchantillonInJSON() {
        return dataService.getAllDataSample();
    }

    @RequestMapping(value = "/Json/dataBetween", method = RequestMethod.GET)
    public @ResponseBody
    DataSample getEchantillonInJSON(@RequestParam("startDate") String startDate,@RequestParam("endDate") String endDate ) {
        Date start = new Date();
        Date end = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
        try {
            start = sdf.parse(startDate);
            end = sdf.parse(endDate);
            
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return dataService.dataToSample(dataService.averagePerLocation(dataService.getDataBetween(start, end)));

    }
    @RequestMapping(value = "/Json/location", method = RequestMethod.GET)
    public @ResponseBody
    List<String> getLocationJSON()
    {
        return dataService.getLocation();
    }
    @RequestMapping(value = "/Json/Visualization2D", method = RequestMethod.GET)
    public @ResponseBody
    List<Data> getDataYears(@RequestParam("view") String view,@RequestParam("date") String date,@RequestParam("location") String location){
        
        Date dateYears = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");  
        try {
            dateYears = sdf.parse(date);
           
            
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(dateYears+" + "+location);
        return dataService.getDataView2D(view, dateYears, location);
    }
    
}
